package jj.tt.dd;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * Created by Admin on 1/3 0003.
 */
public class TestAOP {
    public static void main(String[] args) throws NoSuchFieldException, NoSuchMethodException {
        SuperMan superMan = new SuperMan();
        Human human = (Human) MyProxy.getProxyInstance(superMan);
        human.fly();
        System.out.println();
        human.show();
        Class clazz = SuperMan.class;
//        Field field=clazz.getField("name");
//        Constructor constructor=clazz.getConstructor();
//        System.out.println(constructor);
//        System.out.println(field.getName());

        Field[] fields = clazz.getDeclaredFields();
        for (Field field1 : fields) {
            System.out.println(field1);
            System.out.println(field1.getName());
        }
    }
}

//接口
interface Human {
    void show();
    void fly();
}
//被代理类
class SuperMan implements Human {
    public String name;
    private int age;
    public String address;
    public SuperMan(){
        System.out.println("我是构造器");
    }
    @Override
    public void show() {
        System.out.println("这是一个展示的机会");
    }
    @Override
    public void fly() {
        System.out.println("我是超人,我会飞!");
    }
}
class HumanUtil {
    public void method1() {
        System.out.println("这是一个固定的方法一");
    }
    public void method2() {
        System.out.println("这是一个固定的方法二");
    }
}
//代理类
class MyInvocationHandler2 implements InvocationHandler {
    Object object;
    public void setObject(Object object) {
        this.object = object;
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        HumanUtil hu = new HumanUtil();
        hu.method1();
        Object value = method.invoke(object, args);//这是一个克变动的方法,
        hu.method2();
        return value;
    }
}
//动态的创建一个代理类对象
class MyProxy {
    //定义成一个静态的方法可以就可以使用 类名.方法 获取实例对象,而不用new 对象,静态方法的使用;匿名类对象;
    static Object getProxyInstance(Object obj) {
        MyInvocationHandler2 myInvocationHandler2 = new MyInvocationHandler2();
        myInvocationHandler2.setObject(obj);
        return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), myInvocationHandler2);
    }
}